from faker import Faker
from loguru import logger

from api_test_framework.config.settings import DB_CONFIG
from api_test_framework.utils.handle_mysql import HandleMysql


class GenData:
    @staticmethod
    def gen_unregister_phone():
        """这是生成手机号码方法"""
        fk = Faker(locale="zh_CN")
        while True:
            # 第一步：随机生成号码
            phone_num = fk.phone_number()
            # 第二步：查询数据库
            sql = f'select * from tz_user where user_mobile="{phone_num}";'
            result = HandleMysql(**DB_CONFIG).query_data(sql)
            if result is None:
                logger.info(f"生成的手机号码是：{phone_num}")
                return phone_num

    @staticmethod
    def gen_unregister_name():
        """这是生成用户名方法"""
        fk = Faker(locale="zh_CN")
        while True:
            # 第一步：随机生成号码
            user_name = fk.user_name()
            # user_name = fk.pystr(min_chars=4, max_chars=16)
            # 第二步：查询数据库
            sql = f'select * from tz_user where nick_name="{user_name}";'
            result = HandleMysql(**DB_CONFIG).query_data(sql)
            if result is None:
                logger.info(f"生成的用户名是：{user_name}")
                return user_name


if __name__ == '__main__':
    print(GenData().gen_unregister_phone())
    # print(GenData().gen_unregister_name())
    # eval('GenData().gen_unregister_phone()')
